package com.easyedu.dean.controller;

import com.easyedu.core.entity.BaseError;
import com.easyedu.core.entity.po.OpenCourse;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.core.util.R;
import com.easyedu.dean.entity.dto.request.ReqAddArrangeVersion;
import com.easyedu.dean.entity.dto.request.ReqAutoArrange;
import com.easyedu.dean.entity.dto.request.ReqCancelArrange;
import com.easyedu.dean.entity.dto.request.ReqManualArrange;
import com.easyedu.dean.entity.dto.request.ReqQueryVersionHasArrangeCourse;
import com.easyedu.dean.entity.dto.response.QueryArrangeCourseStatus;
import com.easyedu.dean.entity.dto.response.RespOpenCourseRange;
import com.easyedu.dean.service.ArrangeService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liumengran
 * 课程编排
 */
@RestController
@RequestMapping("/dean/arrange")
public class ArrangeController {

     private final ArrangeService arrangeService;

     public ArrangeController(ArrangeService arrangeService) {

          this.arrangeService = arrangeService;
     }

     /**
      * 功能描述: 自动排课
      *
      * @param reqAutoArrange 开课版本
      * @return : com.easyedu.api.util.R
      **/
     @PostMapping("/version/auto")
     public R autoArrange(@RequestBody @Valid ReqAutoArrange reqAutoArrange) {

          List<OpenCourse> openCourseList = arrangeService.autoArrange3(reqAutoArrange.getVersionId());
          if (openCourseList.isEmpty()) {
               return R.ok().message("所有课程都已排好");
          }
          return R.ok().message("存在未排课程").data(openCourseList);
     }

     /**
      * 功能描述: 手动排课</br>
      * 结合 open_course 还有输入的 组成 课程开课时间表
      *
      * @param reqManualArrange 1 手动排课数据集 Data Transfer Object
      * @return : com.easyedu.api.util.R
      **/
     @PostMapping("/version/manual")
     public R manualArrange(@RequestBody @Valid ReqManualArrange reqManualArrange) {

          arrangeService.manualArrange(reqManualArrange);
          return R.ok();
     }

     /**
      * 取消排课
      *
      * @return 取消排课结果
      */
     @PostMapping("/version/cancel")
     public R cancelArrange(@RequestBody @Valid ReqCancelArrange reqCancelArrange) {

          arrangeService.cancelArrange(reqCancelArrange);
          return R.ok();
     }

     /**
      * 获取排课状态
      *
      * @param versionId 版本id
      * @return
      */
     @GetMapping("/version/status")
     public R arrangeCourseStatus(@RequestParam String versionId) {

          QueryArrangeCourseStatus arrangeCourseStatus = arrangeService.getArrangeCourseStatus(versionId);
          return R.ok().data(arrangeCourseStatus);
     }

     /**
      * 增加排课版本
      */
     @PostMapping("/version/add")
     public R addArrangeVersion(@RequestBody ReqAddArrangeVersion reqAddArrangeVersion) {
          String semester = reqAddArrangeVersion.getSemester();
          if (!(semester.equals("第一学期") || semester.equals("第二学期"))) {
               throw new EasyEduException(BaseError.ERR_PARA, "学期必须为第一学期或第二学期");
          }
          arrangeService.addArrangeVersion(reqAddArrangeVersion.getOpenYear(), semester,
                  reqAddArrangeVersion.getVersionName(), reqAddArrangeVersion.getClassRoomPrice());
          return R.ok();
     }

     /**
      * 获取排课版本
      */
     @GetMapping("/version/list")
     public R getArrangeVersion(@RequestParam int openYear, @RequestParam String semester) {
          return R.ok().data(arrangeService.getArrangeVersion(openYear, semester));
     }

     /**
      * 根据版本查询排课状态索引
      *
      * @param versionId 开课版本
      * @return 课程版本列表
      */
     @GetMapping("/version/timetable/index")
     public R getArrangeVersionInfo(@RequestParam String versionId) {
          RespOpenCourseRange openCourseTimeByVersion = arrangeService.getOpenCourseTimeByVersion(versionId);
          return R.ok().data(openCourseTimeByVersion);
     }

     /**
      * 根据版本获取开课信息
      *
      * @param reqQueryVersionHasArrangeCourse
      * @return
      */
     @PostMapping("/version/timetable/info")
     public R getArrangeTimeTableByVersion(
             @RequestBody @Validated ReqQueryVersionHasArrangeCourse reqQueryVersionHasArrangeCourse) {
          return R.ok().data(arrangeService.getHasArrangeCourseInfoByVersion(reqQueryVersionHasArrangeCourse));
     }

     /**
      * 将排课版本应用到所有课程
      *
      * @param versionId
      * @return
      */
     @PostMapping("/version/timetable/apply")
     public R applyArrange(@RequestParam String versionId) {
          arrangeService.applyArrange(versionId);
          return R.ok();
     }

     /**
      * 删除存在的课表
      * @param openYear 开课年
      * @param semester 开课学期
      * @return 返回成功
      */
     @PostMapping("/version/timetable/delete")
     public R deleteArrange(@RequestParam String openYear, @RequestParam String semester) {
          arrangeService.deleteArrange(openYear, semester);
          return R.ok();
     }
}
